//var id;
var app = angular.module("goodsApp",[]);
app.controller("goodsController",function($scope,$http){
	$scope.initView = function(){
	plus.nativeUI.showWaiting("加载中...");
	$http({
		method:'post',
		url:apiRoot,
		data:{
			action:'Trade.goodInfo',
			id:collection_id,
			uid:attention_id
		}
	}).then(function successCallback(response){
		plus.nativeUI.closeWaiting();
		$scope.goodsData = response.data.data//标题下的数据
		$scope.showBtn = 1;
		console.log(JSON.stringify($scope.goodsData));
	},function errorfunction(e){ 
		console.log(JSON.stringify(e));
	})
	
	//判断是否是关注的状态	
	$http({
		method:'post',
		url:apiRoot,
		data:{
			action:'Community.checkFollowStatus',
			uid:uid,
			type:2,
			type_id:attention_id
		}
	}).then(function successCallback(response){
		if(response.data.error == 121){
			$scope.attention = 121;
		}else{
			$scope.attention = 115;/*显示取消关注*/
		}		
	},function errorfunction(e){ 
		console.log(JSON.stringify(e));
	})	
	
	
	/*关注/取消关注*/
	$scope.isattention = function(way){
		plus.nativeUI.showWaiting('');
		$http({
			method:'post',
			url:apiRoot,
			data:{
				action:'Community.follow',
				uid:uid,
				type:2,
				type_id:attention_id
			}
		}).then(function successCallback(response){
			plus.nativeUI.closeWaiting();
			if(response.data.error == 115){
				/*操作成功*/
				if(way == 2){
					$scope.attention = 121;/*显示关注*/
					toast("已取消关注");
				}else{
					$scope.attention = 115;/*显示取消关注*/
					toast("已关注");
				}				
			}
		},function errorfunction(e){ 
			errortoast();
			console.log(JSON.stringify(e));
		})			
	}
	
	//判断是否是收藏的状态	
	$http({
		method:'post',
		url:apiRoot,
		data:{
			action:'Community.checkCollectStatus',
			uid:uid,
			type:1,
			type_id:collection_id
		}
	}).then(function successCallback(response){
		if(response.data.error == 121){
			$scope.collection = 121;
		}else{
			$scope.collection = 115;/*显示取消收藏*/
		}		
	},function errorfunction(e){ 
		console.log(JSON.stringify(e));
	})	
}  


	/*收藏/取消收藏*/
	var is_clickCollection = true;
	$scope.iscollection = function(way){
		if(is_clickCollection){
		plus.nativeUI.showWaiting('');
		$http({
			method:'post',
			url:apiRoot,
			data:{
				action:'Community.collection',
				uid:uid,
				type:1,
				type_id:collection_id
			}
		}).then(function successCallback(response){
			plus.nativeUI.closeWaiting();
			if(response.data.error == 115){
				/*操作成功*/
				if(way == 1){
					$scope.collection = 121;/*显示关注*/
					toast("已取消收藏");
				}else{
					$scope.collection = 115;/*显示取消关注*/
					toast("已收藏");
				}				
			}
		},function errorfunction(e){ 
			errortoast();
			console.log(JSON.stringify(e));
		})
		is_clickCollection = false;
			setTimeout(function(){
				is_clickCollection = true;
			},2000)
		}
	}
	
	
	$scope.shopping=function(goodid,$type){
		plus.webview.create('confirm_order.html', 'confirm_order.html',{},{goodId:goodid,pay_type:$type}).show('pop-in');
	}
 	$scope.seller_info = function($id){
		plus.webview.create('input_logistics_info.html', 'input_logistics_info.html',{},{uid:$id}).show('pop-in');
  	}
	
	//猜你喜欢的商品详情
	$scope.info = function(uid,type,id){
		plus.webview.create('goods_detail.html','goods_detail.html',{},{gUid:uid,type:type,gId:id}).show('pop-in');
	}
	//打开个人主页
	$scope.icon_sub =function(id,img,nick,type_id){
		plus.webview.create('honer_page.html', 'honer_page.html',{},{icon_id:id,icon_img:img,icon_name:nick,type_id:type_id}).show('pop-in');
	}
	
	//查看评价列表
	$scope.open_evaluateList = function(orderId){
		plus.webview.create('evaluate_list.html', 'evaluate_list.html',{},{order_id:orderId}).show('pop-in');
	}
	
	//跳转到举报页面
	$scope.report = function (uid,type) {
		console.log(uid+"#####"+type);
		plus.webview.create('report.html','report.html',{},{reportUid:uid,reportType:type}).show('pop-in');/*更多商品*/
	}
})

var ws,type_id,type,attention_id,collection_id;
document.addEventListener("plusready",function(){
	appElement=document.querySelector('[ng-controller=goodsController]');
	$scope= angular.element(appElement).scope();
	ws = plus.webview.currentWebview();
	attention_id = ws.gUid;/*对象id，商品、人、图文、视频*/
	collection_id = ws.gId;
	type = ws.type;/*类型*/
	uid = plus.storage.getItem('uid');
	var my = ws.my;
	if(my == 'my'){
		$('#shopping').css('display','none');
	} 
	$scope.initView();
	$scope.$apply();
})
